require_relative 'boot'

require 'rails/all'

require 'rack/redis_throttle'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module BuildAnApiRailsDemo
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    config.autoload_paths << Rails.root.join('app/policies')
    # Limit the daily number of requests to 3
    # 为了测试我们把 limit 设置为 3
    config.middleware.use Rack::RedisThrottle::Daily, max: 300
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # When using Rails 5, remove the quotes
    # [Rails5 对 CORS的配置](https://github.com/cyu/rack-cors#rails-configuration)
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :put, :patch, :delete, :options, :head]
      end
    end

  end
end
